

const writeConfig = {
    sql: `INSERT INTO server_logs ( level, host, logItem, time) VALUES (?,?,?,?)`
}


// 获取日志中的 host，level，time
function getLevelAndHostAndTime(log) {

  if(!log) return null
  if(Object.prototype.toString.call(log).match(/\[object (.*?)\]/)[1].toLowerCase() === 'object') {
    return {
      level: log.level,
      host: log.hostname,
      time: log.time
    }
  }else if ( Object.prototype.toString.call(log).match(/\[object (.*?)\]/)[1].toLowerCase() === 'string') {
    const logT = JSON.parse(log)
    const { level, hostname, time} = logT

    return {
      level,
      host: hostname,
      time
    }

  }
  return null
}






// This is a mock function that simulates writing to a database
module.exports.writeToDatabase = (log) => {
  // 将日志写入数据库
  // return new Promise( async (resolve, reject) => {
  //   const getData = getLevelAndHostAndTime(log)
  //   if(!getData) return reject({
  //     message: '获取日志中的level, host, time失败' 
  //   })
  //   try {
  //     const  [result] = await connection.query(writeConfig.sql, [`${getData.level}`,`${getData.host}`, `${log}`, `${getData.time}`])
  //     // console.log(result, '写入')
  //     resolve(result)
  //   }
  //   catch(error) {
  //     reject({error})
  //   }
  // })
}
